package com.plan.code;

import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;

import java.util.Map;

/**
 * @description: 策略模式+表驱动
 * @author: admin
 * @time: 2022/12/8 22:01
 */
public class IfElse05 {

    public static void main(String[] args) {
        String name = "张三";
        //第一次优化
        noDesign(name);
        //第二次优化
        noDesignOptimization(name);
        //第三次优化
        Handler invokeStrategy = Factory.getInvokeStrategy(name);
        invokeStrategy.AAA(name);
    }

    public static void noDesign(String name) {
        if ("张三".equals(name)) {
            System.out.println("张三完成了AAA任务");
        } else if ("李四".equals(name)) {
            System.out.println("李四完成了AAA任务");
        } else if ("王五".equals(name)) {
            System.out.println("王五完成了AAA任务");
        } else if ("赵六".equals(name)) {
            System.out.println("赵六完成了AAA任务");
        } else {
            System.out.println("没有任务");
        }
    }


    public static void noDesignOptimization(String name) {
        if ("张三".equals(name)) {
            new ZhangSanHandler().AAA(name);
        } else if ("李四".equals(name)) {
            new LiSiHandler().AAA(name);
        } else if ("王五".equals(name)) {
            new WangWuHandler().AAA(name);
        } else if ("赵六".equals(name)) {
            new ZhaoLiuHandler().AAA(name);
        } else {
            System.out.println("没有任务");
        }
    }

}


class Factory {
    private static Map<String, Handler> strategyMap = Maps.newHashMap();

    public static Handler getInvokeStrategy(String name) {
        return strategyMap.get(name);
    }

    //表驱动
    public static void register(String name, Handler handler) {
        if (StringUtils.isEmpty(name) || null == handler) {
            return;
        }
        strategyMap.put(name, handler);
    }
}

interface Handler extends InitializingBean {

     void AAA(String name);
}

class ZhangSanHandler implements Handler {

    @Override
    public void AAA(String name) {
        System.out.println("张三完成了AAA任务");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Factory.register("张三", this);
    }
}

class LiSiHandler implements Handler {

    @Override
    public void AAA(String name) {
        System.out.println("李四完成了AAA任务");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Factory.register("李四", this);
    }
}

class WangWuHandler implements Handler {

    @Override
    public void AAA(String name) {
        System.out.println("王五完成了AAA任务");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Factory.register("王五", this);
    }
}

class ZhaoLiuHandler implements Handler {

    @Override
    public void AAA(String name) {
        System.out.println("赵六完成了AAA任务");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Factory.register("赵六", this);
    }
}